/*
 * GNationalAssociation.java
 * Created on 19 juin 07
 *
 * Glacéo Internet Platform
 * http://sourceforge.net/projects/chm/
 *
 * Copyright (c) 2005-2007, Jan Janke (VirtualHockey Project)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hockeo.vhbase.data.dao.assoc;

import java.util.Locale;

import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;

import com.hockeo.vhbase.data.dao.IGEntity;
import com.hockeo.vhbase.data.dao.contest.GNationalCup;

/**
 * Represents a national hockey association.
 *
 * @version $Id$
 * @author jjanke
 */
@Entity
@DiscriminatorValue( "NATIONAL" )
public class GNationalAssociation extends GAssociation
{
  @ManyToOne( fetch = FetchType.LAZY )
  @JoinColumn( name = "nat_parent_assoc_id" )
  private GInternationalAssociation d_parentAssociation;

  @Column( name = "nat_member_countries" )
  private String                    d_strMemberCountries;

  /**
   * Member countries as a string array (internal variable).
   */
  private transient String[]        d_astrMemberCountries;

  /** Parameterless default constructor. */
  protected GNationalAssociation()
  {
    super();
  }

  /**
   * Overridden to return the default implementation class name, if no specific one is
   * defined.
   */
  @Override
  public String getImplClass()
  {
    String strImplClass = super.getImplClass();

    if ( strImplClass == null )
      strImplClass = "defaultNationalAssocImpl";

    return strImplClass;
  }

  public GInternationalAssociation getParentAssociation()
  {
    return d_parentAssociation;
  }

  /**
   * Returns the ID of the {@link GNationalCup} contest that is attached to this national
   * association.
   */
  public String getNationalCupId()
  {
    return getId() + "0";
  }

  /**
   * Returns the name of the {@link GNationalCup} contest that is attached to this
   * national association.
   */
  public String getNationalCupId( Locale locale )
  {
    return translate( getNationalCupId(), locale );
  }

  /**
   * Returns a comma separated list of all countries (ISO country codes) that are members
   * of this national association. Although a national association is targeted at a single
   * country, some countries adhere to the league/cup system of other countries (e.g.
   * Austria and Slovenia).
   */
  public String getMemberCountries()
  {
    return d_strMemberCountries;
  }

  /**
   * Returns a comma-separated list of localised country names. This method complements
   * {@link #getMemberCountries()} that returns only two letter ISO codes.
   *
   * @param locale the locale to be used for translation
   * @return comma-separated list of country names (the order of the countries is the same
   *         as for the ISO codes)
   */
  public String getMemberCountries( Locale locale )
  {
    if ( d_astrMemberCountries == null )
      d_astrMemberCountries = d_strMemberCountries.split( "\\," );

    StringBuilder sb = new StringBuilder();
    for ( String strCode : d_astrMemberCountries )
    {
      if ( sb.length() > 0 )
        sb.append( "," );

      sb.append( translate( strCode, locale ) );
    }

    return sb.toString();
  }

  public Class<? extends IGEntity> getGEntityClass()
  {
    return GNationalAssociation.class;
  }
}
